#include <buola/gui.h>

#include <buola/gui/ccoloricon.h>

#include <buola/image/cgc.h>
#include <buola/image/cimage.h>

namespace buola { namespace gui {

std::unordered_map<img::CColor,PIcon> CColorIcon::mColorIcons;
mutex CColorIcon::mStaticMutex;

CColorIcon::CColorIcon(const img::CColor &pColor)
    :   mColor(pColor)
{}

CColorIcon::~CColorIcon()
{}

bool CColorIcon::Draw(img::CGC *pGC,const CPoint_d &pPos,int pSize,EStyle /*pStyle*/)
{
    pGC->Path().Rect(pPos,CSize_d(pSize,pSize));
    pGC->SetSource(mColor);
    pGC->FillPath();

    return true;
}

img::CPicture CColorIcon::GetPixmap(int pSize,EStyle /*pType*/)
{
    return img::CPicture(img::CImage_rgb8(CSize_i(pSize,pSize),mColor));
}

PIcon CColorIcon::Get(const img::CColor &pColor)
{
    lock_guard<mutex> lLock(mStaticMutex);

    PIcon &lIcon=mColorIcons[pColor];

    if(!lIcon)
    {
        lIcon=new CColorIcon(pColor);
    }

    return lIcon;
}

/*namespace gui*/ } /*namespace buola*/ }
